<?php

namespace app\api\controller;

use app\BaseController;
use app\Request;
use app\common\lib\sms\AliSms;

class Sms extends BaseController
{
    public function  code(request $request){
        //接值
        $phone = $request->post('phone','','trim,int');

        //验证
        if ($phone == ''){
            return json(['code'=>1,'msg'=>'手机号不能为空','data'=>'']);
        }

        if (strlen($phone) != 11){
            return json(['code'=>1,'msg'=>'请填写正确的手机号','data'=>'']);
        }

        //验证码
        $rand = rand(100000,999999);

        $res = AliSms::sendCode($phone,$rand);

        if ($res){
            return json(['code'=>0,'msg'=>'验证码发送成功','data'=>'']);
        }

        return json(['code'=>1,'msg'=>'验证码发送失败','data'=>'']);
    }
}